const dateUtils = {};

dateUtils.getExpirationTime = (temper) => {
    const defDate = new Date();
    if (temper) {
        temper = temper.replace(/-/g, "/");
        const date = new Date(temper);
        if (date > defDate) {
            return dateformat(date, "yyyy-MM-dd");
        }
    }
    return dateformat(defDate, "yyyy-MM-dd");
};

function dateformat(date, fmt) {
    let o = {
        "M+": date.getMonth() + 1,               //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (let k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}


/**
 *获取下一个月的输入日期
 *{param:DateTime} date 输入日期(YYYY-MM-DD)
 *{param:number } monthNum 月数
 */
dateUtils.getNextMonthDay = (date, monthNum) => {
    const dateArr = date.split('-');
    const year = dateArr[0]; //获取当前日期的年份
    const month = dateArr[1]; //获取当前日期的月份
    const day = dateArr[2]; //获取当前日期的日
    let year2 = year;
    let month2 = parseInt(month) + parseInt(monthNum);
    if (month2 > 12) {
        year2 = parseInt(year2) + parseInt((parseInt(month2) / 12 === 0 ? 1 : parseInt(month2) / 12));
        month2 = parseInt(month2) % 12;
    }
    let day2 = day;
    let days2 = new Date(year2, month2, 0);
    days2 = days2.getDate();
    if (day2 > days2) {
        day2 = days2;
    }
    if (month2 < 10) {
        month2 = '0' + month2;
    }
    return year2 + '-' + month2 + '-' + day2;
};


dateUtils.addDayDate = (date, AddDayCount) => {
    let dd = new Date(date);
    dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
    let y = dd.getFullYear();
    let m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期，不足10补0
    let d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
    return y + "-" + m + "-" + d;
};

export default dateUtils;